﻿using System;
using DMS.Business.Utility;
using DMS.BaseClass;
using DMS.Business.Modules.Organization;

namespace DMS.Business.Modules.SupplierManagement
{
    [Serializable]
    public class SupplierAccount : AuthenticationUser
    {
        #region 数据成员
        protected string _supplierID = "";
        protected string _supplier = "";
        protected Supplier _Company = null;
        protected bool _IsDefaultAccount = false;
        protected DateTime _ValidDate = DateTime.MinValue;
        protected DateTime _LastPasswordModify = DateTime.MinValue;
        protected string _ProjectTypeID = "";
        protected string _ProjectTypeName = "";
        protected string _Position = "";
        protected bool _IsManager = false;
        protected bool _IsDeleted = false;
        #endregion

        #region 成员属性
        /// <summary>
        /// 成员类型
        /// </summary>
        public override ActorTypes ActorType
        {
            get
            {
                return ActorTypes.SupplierAccount;
            }
        }
        /// <summary>
        /// 供应商ID
        /// </summary>
        public string SupplierID
        {
            get { return _supplierID; }
            set { _supplierID = value; }
        }
        /// <summary>
        /// 供应商名称
        /// </summary>
        public string Supplier
        {
            get { return _supplier; }
            set { _supplier = value; }
        }
        /// <summary>
        /// 供应商
        /// </summary>
        public Supplier Company
        {
            get 
            {
                if (this._Company == null && this._supplierID != "")
                {
                    this._Company = SupplierHelper.GetSupplierByID(this._supplierID);
                }

                return _Company; 
            }
            set 
            {
                if (value == null)
                {
                    this._supplierID = "";
                }
                else
                {
                    this._supplierID = value.ID.ToString();
                }

                _Company = value; 
            }
        }
        /// <summary>
        /// 是否默认帐号
        /// </summary>
        public bool IsDefaultAccount
        {
            get { return _IsDefaultAccount; }
            internal set { _IsDefaultAccount = value; }
        }
        /// <summary>
        /// 帐号有效期
        /// </summary>
        public DateTime ValidDate
        {
            get { return _ValidDate; }
            set { _ValidDate = value; }
        }
        /// <summary>
        /// 密码最后修改时间
        /// </summary>
        public DateTime LastPasswordModify
        {
            get { return _LastPasswordModify; }
            set { _LastPasswordModify = value; }
        }
        public string ProjectTypeID
        {
            get { return _ProjectTypeID; }
            set { _ProjectTypeID = value; }
        }

        public string ProjectTypeName
        {
            get { return _ProjectTypeName; }
            set { _ProjectTypeName = value; }
        }
        /// <summary>
        /// 岗位
        /// </summary>
        public string Position
        {
            get { return _Position; }
            set { _Position = value; }
        }
        /// <summary>
        /// 是否管理员
        /// </summary>
        public bool IsManager
        {
            get { return _IsManager; }
            set { _IsManager = value; }
        }
        public bool IsDeleted
        {
            get { return _IsDeleted; }
            set { _IsDeleted = value; }
        }

        #endregion

        #region 构造函数
        public SupplierAccount()
        {
        }
        public SupplierAccount(string id)
            : base(id)
        {
        }

        public SupplierAccount(string loginID, Supplier company):base("")
        {
            if (SupplierHelper.GetAccountByLoginID(loginID) != null)
            {
                string msg = string.Format("该帐号({0})已被使用，登录帐号不能重复", loginID);
                throw new LogicException(msg);
            }

            this._LoginID = loginID;
            this.Company = company;
        }
        #endregion

        #region 成员方法
        /// <summary>
        /// 保存
        /// </summary>
        public override void Save()
        {
            if (string.IsNullOrEmpty(_ID))
            {
                this._ID = Guid.NewGuid().ToString();
                
                SupplierHelper.AddSupplierAcount(this);
            }
            else
            {
                SupplierHelper.UpdateSupplierAccount(this);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete()
        {
            if (this._ID == "")
            {
                throw new InvalidOperationException("删除失败，该帐号还未保存，无法删除。");
            }

            SupplierHelper.DeleteSupplierAccount(this);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="newPassword">新密码</param>
        public override void ResetPassword(string newPassword)
        {
            this._LastPasswordModify = DateTime.Now;
            base.ResetPassword(newPassword);
        }
        #endregion
    }
}
